package com.rayo.accesscontrollercopy.module.setting.presenter;

import android.content.Context;
import android.os.Message;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.app.BasePresenter;
import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.model.biz.IParamBiz;
import com.rayo.accesscontrollercopy.model.biz.ParamBiz;
import com.rayo.accesscontrollercopy.model.callback.OnGetDbListener;
import com.rayo.accesscontrollercopy.model.callback.OnUpdateDbListener;
import com.rayo.accesscontrollercopy.model.db.DbHelp;
import com.rayo.accesscontrollercopy.model.db.bean.SystemData;
import com.rayo.accesscontrollercopy.module.setting.view.IParamView;
import com.rayo.accesscontrollercopy.utils.PreferencesUtils;
import com.rayo.wallreader.gpio.OnRelayListener;
import com.rayo.wallreader.gpio.RelayBiz;

/**
 * Created by yanghaojie on 16/7/25.
 */
public class ParamPresenter extends BasePresenter {
    private IParamView iParamView;
    private IParamBiz iParamBiz;
//    private IReaderBiz iReaderBiz;

    private static final int INIT = 1;
    private static final int SHOW_SNACK_BAR = 2;
    private static final int SHOW_DIALOG = 3;
    private static final int RESTART_APP = 4;
    private int ac=0;
    private Context mContext;
    /**
     * @param context
     * @param iParamView
     */
    public ParamPresenter(Context context, IParamView iParamView) {
        init(context);
        mContext=context;
        this.iParamView = iParamView;
        this.iParamBiz = new ParamBiz();
    }

    /**
     * @param msg
     */
    @Override
    protected void message(Message msg) {
        switch (msg.what) {
            case INIT:
                iParamView.showProgressBar(false);
                SystemData systemData = (SystemData) msg.obj;
                if (null != systemData)
                    iParamView.setParamInfo(systemData.getDateFormat(), systemData.getLang(),
                            systemData.getNameFormat(), systemData.getTimeliness(), systemData.getDelay());
                break;
            case SHOW_SNACK_BAR:
                iParamView.showSnackBar((String) msg.obj);
                break;
            case SHOW_DIALOG:
                iParamView.showAlertDialog((String) msg.obj);
                break;
            case RESTART_APP:

                iParamView.restartApp();
                break;
            default:
                break;
        }
    }

    /**
     *
     */
    private OnUpdateDbListener onUpdateDbListener = new OnUpdateDbListener() {
        @Override
        public <T> void onSuccess(String action, T t) {

            if (SystemData.class.getSimpleName().equals(t.getClass().getSimpleName())) {
                GlobalVariables.getInstance().setmSystemData((SystemData) t);
                obtainMessage(SHOW_SNACK_BAR, reference.get().getString(R.string.setting_save_successfully)).sendToTarget();
                if(ac==1){
                    obtainMessage(RESTART_APP).sendToTarget();
                }
            } else
                obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.data_wrong)).sendToTarget();
        }

        @Override
        public void onFailed(String action, String error) {
            obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.setting_save_failed)).sendToTarget();
        }
    };

    /**
     *
     */
    @Override
    public void initData() {
        iParamBiz.init(new OnGetDbListener() {
            @Override
            public <T> void onSuccess(T t) {
                if (SystemData.class.getSimpleName().equals(t.getClass().getSimpleName()))
                    obtainMessage(INIT, t).sendToTarget();
                else
                    obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.data_wrong)).sendToTarget();
            }

            @Override
            public void onFailed(int error) {
                obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.setting_get_failed)).sendToTarget();
            }
        });
    }

    /**
     * @param pos
     */
    public void saveDtFormat(int pos) {
        SystemData systemData = GlobalVariables.getInstance().getmSystemData();
        systemData.setDateFormat(pos);
        ac=0;
        iParamBiz.saveParam(systemData, onUpdateDbListener);

    }

    /**
     * @param pos
     */
    public void saveLang(int pos) {
        SystemData systemData = GlobalVariables.getInstance().getmSystemData();
        systemData.setLang(pos);
        ac=1;
        iParamBiz.saveParam(systemData, onUpdateDbListener);

    }

    /**
     * @param pos
     */
    public void saveNameOrder(int pos) {
        SystemData systemData = GlobalVariables.getInstance().getmSystemData();
        systemData.setNameFormat(pos);
        ac=0;
        iParamBiz.saveParam(systemData, onUpdateDbListener);

    }

    /**
     * @param pos
     */
    public void saveTimeliness(int pos) {
        SystemData systemData = GlobalVariables.getInstance().getmSystemData();
        systemData.setTimeliness(pos);
        ac=0;
        iParamBiz.saveParam(systemData, onUpdateDbListener);

    }

    /**
     * @param second
     */
    public void saveOpenTime(int second) {
        SystemData systemData = GlobalVariables.getInstance().getmSystemData();
        systemData.setDelay(second);
        ac=0;
        iParamBiz.saveParam(systemData, onUpdateDbListener);

    }

    /**
     *
     */
    public void getVersion() {
        obtainMessage(SHOW_DIALOG, iParamBiz.getVersion(reference.get())).sendToTarget();
    }

    /**
     *
     */
    public void officeMode() {
        obtainMessage(SHOW_DIALOG, iParamBiz.getOfficeMode(reference.get())).sendToTarget();
    }


    /**
     *
     */
    public void openController() {
        RelayBiz relayBiz = new RelayBiz(RelayBiz.RELAY1);

        relayBiz.openRelaySec(GlobalVariables.getInstance().getmSystemData().getDelay(), new OnRelayListener.OnOpenListener() {
            @Override
            public void openSuccess() {
                obtainMessage(SHOW_SNACK_BAR, reference.get().getString(R.string.open_successfully)).sendToTarget();
            }

            @Override
            public void openFailed() {
                obtainMessage(SHOW_SNACK_BAR, reference.get().getString(R.string.open_failed)).sendToTarget();
            }

            @Override
            public void closeSuccess() {
                obtainMessage(SHOW_SNACK_BAR, reference.get().getString(R.string.close_successfully)).sendToTarget();
            }

            @Override
            public void closeFailed() {
                obtainMessage(SHOW_SNACK_BAR, reference.get().getString(R.string.close_failed)).sendToTarget();
            }
        });
//        iReaderBiz = new ReaderBiz();
//        iReaderBiz.open(ReaderBiz.RELAY_AUTO, new OnRelayListener.OnOpenListener() {
//            @Override
//            public void openSuccess() {
//                obtainMessage(SHOW_SNACK_BAR, reference.get().getString(R.string.open_successfully)).sendToTarget();
//            }
//
//            @Override
//            public void openFailed() {
//                obtainMessage(SHOW_SNACK_BAR, reference.get().getString(R.string.open_failed)).sendToTarget();
//            }
//
//            @Override
//            public void closeSuccess() {
//
//            }
//
//            @Override
//            public void closeFailed() {
//
//            }
//        });
    }

    public void reset() {
        DbHelp.liteOrm.deleteDatabase();
        PreferencesUtils.clear(mContext);
        obtainMessage(RESTART_APP).sendToTarget();
    }
}
